# coding=utf-8
import re,sys,requests
#from xls import Xls
import xlrd
from xlutils.copy import copy
#from MapApi import Api
"""
先使用All Office Converter Platinum 将文档转txt 在cmd下 dir /b >1.txt 在运行脚本，可能存在编码问题
v 1.0 代码略冗余 w(ﾟДﾟ)w
by:bigmimi
"""
reload(sys)
sys.setdefaultencoding('utf-8')
#事件名称	事件来源	域名	IP	漏洞类型	行业类别	事件单位名称	事件单位地址	上级单位名称	上级单位地址
# 事件名称5   域名9 ip 9 事件单位名称 7
def NR(file,num):
	#mm = Xls()
	with open(file,'r') as fo:
		a = fo.readlines()
		tit = a[4].decode('gbk').encode('utf-8')  # 漏洞标题
		writeExcel(num, 0, unicode(tit))
		dw = a[6].decode('gbk').encode('utf-8')
		dw2 = dw.split('：') #漏洞单位
		writeExcel(num,6,unicode(dw2[0]))
		Gd(num, 7, unicode(dw2[0]))  # 定位漏洞单位地址
		sjly =u'自主发现'
		writeExcel(num, 1, sjly)#来源
		hylb = u'政府'
		writeExcel(num, 5, hylb)#类型
		print tit,dw2[0]
		#CS(file,num)
def CS(file,num):
	#mm = Xls()
	f = open(file)
	str = f.read().decode('gbk').encode('utf-8')
	un = unicode(str)
	s = u"抄送：(.*)"
	a = re.findall(s,un)
	print a[0]
	Gd(num, 9, (a[0])) #定上级位地址
	writeExcel(num, 8, unicode(a[0]))
	ip = re.findall(r'(\d+\.\d+\.\d+\.\d+)', un, re.S)
	print ip[0]
	writeExcel(num, 3, unicode(ip[0]))
	s2 = u"事件.*?([A-Za-z0-9].*?)  IP"
	dome = re.findall(s2, un,re.S)#多行，坑
	print dome[0]
	writeExcel(num, 2, unicode(dome[0]))
	f.close()

def writeExcel(row, col, nr):
	'''
	:param row: 行
	:param col: 列
	:param nr: 内容
	:return:
	'''
	rb = xlrd.open_workbook('3.xls', formatting_info=True)
	wb = copy(rb)
	ws = wb.get_sheet(0)  # 页码
	ws.write(row, col, nr)  # 行数，列数，内容，
	wb.save('3.xls')

def Gd(num,col,dm):
	'''
	:param num:
	:param col:
	:param dm:
	:return:
	'''
	head = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0'}
	try:
		url = 'http://ditu.amap.com/service/poiInfo?query_type=TQUERY&pagesize=20&pagenum=1&qii=true&cluster_state=5&need_utd=true&utd_sceneid=1000&div=PC1000&addr_poi_merge=true&is_classify=true&zoom=12&city=500000&geoobj=106.383082%7C29.456058%7C106.736705%7C29.616163&keywords=' +str(dm)
		n = requests.get(url=url,headers=head,timeout=10)
		dist = n.text
		#print dist
		dmbm = re.findall(r'"diner_flag":"0","id":"(.*?)","name":',dist,re.M)
		#print dmbm[0] #地图编号
		dz = requests.get(url='http://ditu.amap.com/detail/get/detail?id=%s' % dmbm[0], headers=head, timeout=10)
		dzz = dz.text
		d = re.findall(r'address":"(.*?)"',dzz,re.M)
		print d[0]#物理地址
		writeExcel(num, col, unicode(d[0]))
	except Exception,e:
		print e
file = open('1.txt','r')
n = 0
for line in file.readlines():
	n += 1
	line = line.strip('\n')
	li = unicode(line)
	print li,n
	NR(li,n)
	CS(li,n)
